#include "game1.h"
#include "ui_game1.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QMouseEvent>

map2 map22;
QTimer *timer2=new QTimer();
int speed2=500;

Game1::Game1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Game1)
{
    ui->setupUi(this);
    connect(timer2,&QTimer::timeout,this,&Game1::timerUpdate);
    timer2->stop();
}

Game1::~Game1()
{
    delete ui;
}

void Game1::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //设置网格颜色
    QPen pen(Qt::gray);
    painter.setPen(pen);

    //以label左上角为原点
    painter.translate(ui->label->x(),ui->label->y());

    //清空画布
       QBrush brush(Qt::white,Qt::SolidPattern);
       painter.setBrush(brush);
       painter.drawRect(0,0,ui->label->width()-1,ui->label->height()-1);

    //绘制网格
    for(int x=0;x<=map22.getwidthnum();x++)
    {
        painter.drawLine(x*map22.getwidth(),0,x*map22.getwidth(),map22.getheightnum()*map22.getheight());
    }
    for(int y=0;y<=map22.getheightnum();y++)
    {
        painter.drawLine(0,y*map22.getheight(),map22.getwidthnum()*map22.getwidth(),y*map22.getheight());
    }

    //绘制细胞
    brush.setColor(Qt::black);
    painter.setBrush(brush);
    for(int x=0;x<map22.getwidthnum();x++)
    {
        for(int y=0;y<map22.getheightnum();y++)
        {
            if(map22.getmap(x,y)==1)
                    painter.drawRect(x*map22.getwidth(),y*map22.getheight(),map22.getwidth(),map22.getheight());
        }
    }
    painter.end();
}

void Game1::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map22.getwidth()*map22.getwidthnum() and point.y()>=0 and point.y()<=map22.getheight()*map22.getheightnum())//防止超出范围
        {
            lastcellx=point.x()/map22.getwidth();
            lastcelly=point.y()/map22.getheight();
            map22.changemap(point.x()/map22.getwidth(),point.y()/map22.getheight());//改变细胞状态
            update();//重绘界面
        }
    }
}

void Game1::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map22.getwidth()*map22.getwidthnum() and point.y()>=0 and point.y()<=map22.getheight()*map22.getheightnum())//防止超出map范围
        {
            if(lastcellx!=point.x()/map22.getwidth() or lastcelly!=point.y()/map22.getheight())
            {
                lastcellx=point.x()/map22.getwidth();
                lastcelly=point.y()/map22.getheight();
                map22.changemap(point.x()/map22.getwidth(),point.y()/map22.getheight());
                update();//重绘界面
            }
        }
    }
}

void Game1::timerUpdate()
{
    map22.mapupdate();
    update();//更新界面
}

void Game1::on_startpushButton_clicked()
{
    timer2->start(speed2);
}

void Game1::on_pausepushButton_clicked()
{
    timer2->stop();
}

void Game1::on_clearpushButton_clicked()
{
    map22.mapclear();
    timer2->stop();
    update();
}

void Game1::on_backpushButton_clicked()
{
    this->close();
    study *s =new study();
    s->show();
}

void map2::mapupdate()
{
    int dx[8]={0,0,-1,1,-1,1,-1,1};
    int dy[8]={1,-1,0,0,1,1,-1,-1};

    memset(neighbor,0,sizeof(neighbor));//细胞清零

    //统计邻居细胞数量
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(map[x][y]==1)
            {
                for(int i=0;i<8;i++)
                {
                    if(dx[i]+x>=0 and dx[i]+x<=widthnum and dy[i]+y>=0 and dy[i]+y<=heightnum) //防止越界
                    {
                        neighbor[dx[i]+x][dy[i]+y]++;//
                    }
                }
            }
        }
    }

    //改变细胞状态
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(neighbor[x][y]==3)
                map[x][y]=1;
            else if(neighbor[x][y]!=2)
                map[x][y]=0;
        }
    }
}

void map2::mapclear()
{
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            map[x][y]=0;
        }
    }
}
